//
//  SplashViewController.m
//  SnapCAD
//
//  Created by 暗流澎涌 on 2025/7/25.
//

#import "SplashViewController.h"
#import "HomeViewController.h"

@interface SplashViewController ()
@property (nonatomic, strong) UIImageView *logoImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UIActivityIndicatorView *loadingIndicator;
@end

@implementation SplashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self startLoadingAnimation];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor colorWithRed:0.31 green:0.35 blue:0.91 alpha:1.0]; // indigo-600
    
    // Logo Container
    UIView *logoContainer = [[UIView alloc] init];
    logoContainer.backgroundColor = [UIColor whiteColor];
    logoContainer.layer.cornerRadius = 30;
    logoContainer.layer.shadowColor = [UIColor blackColor].CGColor;
    logoContainer.layer.shadowOffset = CGSizeMake(0, 4);
    logoContainer.layer.shadowOpacity = 0.1;
    logoContainer.layer.shadowRadius = 8;
    logoContainer.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:logoContainer];
    
    // Logo Image (using SF Symbol)
    self.logoImageView = [[UIImageView alloc] init];
    UIImage *logoImage = [UIImage systemImageNamed:@"cube.box.fill"];
    self.logoImageView.image = logoImage;
    self.logoImageView.tintColor = [UIColor colorWithRed:0.31 green:0.35 blue:0.91 alpha:1.0];
    self.logoImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.logoImageView.translatesAutoresizingMaskIntoConstraints = NO;
    [logoContainer addSubview:self.logoImageView];
    
    // Title Label
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"SnapCAD";
    self.titleLabel.font = [UIFont boldSystemFontOfSize:32];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.titleLabel];
    
    // Subtitle Label
    self.subtitleLabel = [[UILabel alloc] init];
    self.subtitleLabel.text = @"Professional CAD Viewer";
    self.subtitleLabel.font = [UIFont systemFontOfSize:16];
    self.subtitleLabel.textColor = [UIColor colorWithWhite:1.0 alpha:0.8];
    self.subtitleLabel.textAlignment = NSTextAlignmentCenter;
    self.subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.subtitleLabel];
    
    // Loading Indicator
    self.loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleLarge];
    self.loadingIndicator.color = [UIColor whiteColor];
    self.loadingIndicator.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.loadingIndicator];
    
    // Auto Layout
    [NSLayoutConstraint activateConstraints:@[
        // Logo Container
        [logoContainer.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
        [logoContainer.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor constant:-60],
        [logoContainer.widthAnchor constraintEqualToConstant:120],
        [logoContainer.heightAnchor constraintEqualToConstant:120],
        
        // Logo Image
        [self.logoImageView.centerXAnchor constraintEqualToAnchor:logoContainer.centerXAnchor],
        [self.logoImageView.centerYAnchor constraintEqualToAnchor:logoContainer.centerYAnchor],
        [self.logoImageView.widthAnchor constraintEqualToConstant:60],
        [self.logoImageView.heightAnchor constraintEqualToConstant:60],
        
        // Title Label
        [self.titleLabel.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
        [self.titleLabel.topAnchor constraintEqualToAnchor:logoContainer.bottomAnchor constant:30],
        
        // Subtitle Label
        [self.subtitleLabel.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
        [self.subtitleLabel.topAnchor constraintEqualToAnchor:self.titleLabel.bottomAnchor constant:8],
        
        // Loading Indicator
        [self.loadingIndicator.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
        [self.loadingIndicator.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor constant:-80]
    ]];
}

- (void)startLoadingAnimation {
    [self.loadingIndicator startAnimating];
    
    // 添加淡入动画
    self.logoImageView.alpha = 0;
    self.titleLabel.alpha = 0;
    self.subtitleLabel.alpha = 0;
    
    [UIView animateWithDuration:1.0 delay:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.logoImageView.alpha = 1.0;
        self.titleLabel.alpha = 1.0;
        self.subtitleLabel.alpha = 1.0;
    } completion:^(BOOL finished) {
        // 3秒后跳转到主页
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self navigateToHome];
        });
    }];
}

- (void)navigateToHome {
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:homeVC];
    navController.navigationBarHidden = YES;
    
    // 设置转场动画
    navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    navController.modalPresentationStyle = UIModalPresentationFullScreen;
    
    [self presentViewController:navController animated:YES completion:nil];
}

@end
